package com.five.admin.controller;

import com.five.controller.BaseController;
import com.five.admin.service.CourseClassifyService;
import com.five.entity.CourseClassify;
import com.five.vo.BaseResultVO;
import com.five.entity.CourseClassify;
import com.five.entity.User;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: star
 * @Date: 2021-05-21 14:13
 *  课程分类 Controller层
 */
@Controller
@RequestMapping("courseClassify")
@Api(tags = "课程分类业务接口")
@CrossOrigin
public class CourseClassifyController extends BaseController {

    @Autowired
    private CourseClassifyService courseClassifyService;

    /**
     * @return课程类型列表
     */
    @ResponseBody
    @RequestMapping(value = "getCourseClassify",method = RequestMethod.GET)
    @ApiOperation("获取课程类型列表接口")
    public BaseResultVO getCourseClassify(String typename,Integer page){
        List<CourseClassify> list = courseClassifyService.fuzzyQueryByTypename(typename,page);
        int pages = ((courseClassifyService.countCourseClassify(typename)-1)/10)+1;
        Map<String,Object> resultMap = new HashMap();
        resultMap.put("pages",pages);
        resultMap.put("courseClassifyList",list);
        return successResp(resultMap);
    }

    /**
     *
     * @return 返回所有课程类别
     */
    @ResponseBody
    @RequestMapping(value = "courseClassifyList",method = RequestMethod.GET)
    @ApiOperation("获取课程类别列表接口")
    public BaseResultVO courseClassifyList(){
        List<CourseClassify> courseClassifyList = courseClassifyService.findAll();
        return successResp(courseClassifyList);
    }

    @ResponseBody
    @RequestMapping(value = "insertCourseClassify",method = RequestMethod.POST)
    @ApiOperation("新增课程类别接口")
    public BaseResultVO insertCourseClassify(String typename){
        courseClassifyService.insertCourseClassify(typename);
        return  successRespWithMessage("成功添加课程类别");
    }

    @ResponseBody
    @RequestMapping(value = "updateCourseClassify",method = RequestMethod.POST)
    @ApiOperation("更新课程类别接口")
    public BaseResultVO updateCourseClassify(CourseClassify courseClassify){
        courseClassifyService.updateCourseClassify(courseClassify);
        return  successRespWithMessage("成功更新课程类别");
    }

    @ResponseBody
    @RequestMapping(value = "findById",method = RequestMethod.POST)
    @ApiOperation("根据id查询课程类别")
    public BaseResultVO findById(long id){
        CourseClassify courseClassify = courseClassifyService.findById(id);
        return  successResp(courseClassify);
    }

    @ResponseBody
    @RequestMapping(value = "deleteById",method = RequestMethod.POST)
    @ApiOperation("根据id删除课程类别")
    public BaseResultVO deleteById(long id){
        courseClassifyService.deleteCourseClassify(id);
        return  successRespWithMessage("成功删除课程类别");
    }

    @ResponseBody
    @RequestMapping(value = "deleteSelect",method = RequestMethod.POST)
    @ApiOperation("删除选中的课程类别")
    public BaseResultVO deleteSelect(@RequestParam(value = "ids[]") long[] ids){
        courseClassifyService.deleteSelectCourseClassify(ids);
        return  successRespWithMessage("成功删除课程类别");
    }

    /**
     * @return课程类型列表
     */
    @ResponseBody
    @RequestMapping(value = "fuzzyQueryCourseClassify",method = RequestMethod.GET)
    @ApiOperation("模糊查询课程类型")
    public BaseResultVO fuzzyQueryCourseClassify(String typename,int page){
        List<CourseClassify> list = courseClassifyService.fuzzyQueryByTypename(typename,page);
        return successResp(list);
    }
}
